#include<bits/stdc++.h>
#define int long long
using namespace std;
const int Maxn = 8010, Maxm = 100000;
int T, n, m;
struct node{
	int x, y, w;
}a[Maxn];
int mp[Maxm + 10], prime[Maxm + 10], vis[Maxm + 10], tot;
void Prime(){
	vis[1] = 1;
	for(int i = 2 ; i <= Maxm ; i++){
		if(!vis[i]) mp[i] = 1, prime[++tot] = i;
		vis[i] = 1;
		for(int j = 1 ; j <= tot && prime[j] * i <= Maxm ; j++){
			vis[i * prime[j]] = 1;
			if(i % prime[j] == 0) break;
		}
	}
}
signed main(){
	ios::sync_with_stdio(0);
	freopen("chess.in", "r", stdin);
	freopen("chess.out", "w", stdout);
	Prime();
	cin >> T;
	while(T--){
		cin >> n >> m;
		for(int i = 1 ; i <= n ; i++)
			cin >> a[i].x >> a[i].y;
		for(int i = 1 ; i <= n ; i++) cin >> a[i].w;
		int ans = 0;
		for(int i = 1 ; i <= n ; i++){
			for(int j = i + 1 ; j <= n ; j++){
				for(int k = j + 1 ; k <= n ; k++){
					int s1 = abs(a[i].x - a[j].x) + abs(a[i].y - a[j].y);
					int s2 = abs(a[i].x - a[k].x) + abs(a[i].y - a[k].y);
					int s3 = abs(a[j].x - a[k].x) + abs(a[j].y - a[k].y);
					int mid;
					if((s1 >= s2 && s1 <= s3) || (s1 >= s3 && s1 <= s2)) mid = s1;
					if((s2 >= s1 && s2 <= s3) || (s2 >= s3 && s2 <= s1)) mid = s2;
					if((s3 >= s1 && s3 <= s2) || (s3 >= s2 && s3 <= s1)) mid = s3;
					if(mp[mid] && (mid % 20 == 3 || mid <= 10)) ans += a[i].w + a[j].w + a[k].w;
				}
			}
		}
		cout << ans << '\n';
	}
	return 0;
}
/*

*/


